#import wizard.addcolumn

from PyQt4.QtGui import *
from docmanager import *
from conversions import tidy, currency
from decimal import Decimal
#from currency import currency

class _Page(QWizardPage):
   def __init__(self, title):
      QWizardPage.__init__(self)
      self.setTitle(title)
      self.setLayout(QGridLayout())

   def addWidget(self, name, label, widget):
      '''Add a widget to the page.'''
      l = QLabel(tidy(label), self)
      l.setBuddy(widget)
      self.layout().addWidget(l, self.layout().rowCount(), 0)
      self.layout().addWidget(widget, self.layout().rowCount() - 1, 1)
      self.registerField(name, widget)

   def addRadioField(self, name, text, default=False):
      btn = QRadioButton(self)
      btn.setChecked(default)
      self.addWidget(name, text, btn)

   def addCheckField(self, name, text, default=False):
      btn = QCheckBox(self)
      btn.setChecked(default)
      self.addWidget(name, text, btn)

   def addTextField(self, name, text, default=''):
      w = QLineEdit(self)
      w.setText(default)
      self.addWidget(name, text, w)

   def addListField(self, name, items, default=0):
      l = QListWidget(self)
      l.addItems(items)
      l.setCurrentRow(default)
      self.layout().addWidget(l)
      self.registerField(name, l)

   def addComboField(self, name, text, items, default=0):
      w = QComboBox(self)
      w.addItems(items)
      w.setCurrentIndex(default)
      self.addWidget(name, text, w)


class AddColumn(QWizard):

   # Which type of column is being added
   listtypes = ('item', 'section', 'schedule')

   def availColumns(self):
      restrict = tuple(filter(lambda x: x != self.listtype, self.listtypes))
      return filter(lambda col: not col.__name__.lower().startswith(restrict), DocManager().availColumns)

   def pageType(self):
      '''Create the page to select which type of column to add'''
      p = _Page('Column Type')
      p.setSubTitle('Select the type of the new column.')
      p.addListField('type', [tidy(col.__name__) for col in self.availColumns()])
      return p

   class pageFields(_Page):
      def __init__(self, availcols):
         _Page.__init__(self, 'Required Fields')
         self.availcols = availcols

      def initializePage(self):
         self.setSubTitle('Enter the following required fields')
         col = self.availcols[self.field('type').toInt()[0]]
         # Add a field for each required key
         for key, value in col.required().iteritems():
            if value.type in (str, int, float, Decimal, currency):
               self.addTextField(key, key,  value.value)
            elif value.type is bool:
               self.addCheckField(key, key, value.value)
            elif isenum(value.type):
               self.addComboField(key, key, [e for e in value.type], 0)
            elif value.type is boqdoc.Column:
               columns = [col.name for col in self.collist]
               self.addComboField(key, key, columns, 0)
               

   def __init__(self, listtype, table):
      '''collist is the list to which the new column is to be added.
      listtype is one of 'item', 'section', 'schedule'.
      '''
      QWizard.__init__(self, qApp.activeWindow())
      assert listtype in ('item', 'section', 'schedule')
      self.listtype = listtype
      self.table = table
      self.coltype = None
      self.setWindowTitle('Add column')
      self.addPage(self.pageType())
      self.addPage(self.pageFields(self.availColumns()))

   def accept(self):
      col = self.availColumns()[self.field('type').toInt()[0]]
      kwargs = dict([(key, str(self.field(key).toString())) for key in col.required().iterkeys()])
      kwargs['keys'] = self.table.rows
      column = boqdoc.Column(self.availColumns()[self.field('type').toInt()[0]], **kwargs)
      self.table.columns.insert(len(self.table.columns), column)
      QWizard.accept(self)


